package com.ztx.web.controller.sys;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.ztx.model.User;
import com.ztx.pagemodel.SessionInfo;
import com.ztx.service.user.IUserService;

@Controller
public class LoginController {

	@Resource
	private IUserService userService = null;

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String login() {
		return "login";
	}

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String login(Model model, @RequestParam("username") String username,
			@RequestParam("password") String password, HttpSession session) {
		User user = userService.login(username, password);
		if (user != null) {
			SessionInfo sessionInfo = new SessionInfo();
			sessionInfo.setUser(user);
			sessionInfo.setResourceList(userService.resourceList(user.getId()));
			session.setAttribute("sessionInfo", sessionInfo);
			return "home";
		} else {
			model.addAttribute("message", "用户名或密码错误");
			return "login";
		}
	}
}
